package com.kk.app.utils;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;


/**
 * 任务帮助，可以随时切换任务在工作线程还是ui线程运行
 */
public class TasksHelper {

    private static Handler sUIHandler;

    private static Handler sWorkHandler;
    private static HandlerThread sWorkThread;

    public static synchronized Handler getWorkHandler(){
        if(sWorkThread==null)
        {
            sWorkThread = new HandlerThread("task");
            sWorkThread.start();
            sWorkHandler = new Handler(sWorkThread.getLooper());
        }
        return sWorkHandler;
    }


    public static synchronized Handler  getUIHandler(){
         if(sUIHandler==null)
         {
             sUIHandler = new Handler(Looper.getMainLooper());
         }
         return sUIHandler;
    }

    public static  void runUITask(Runnable runnable)
    {
        Handler handler =  getUIHandler();
        if(handler!=null)
        {
            handler.post(runnable);
        }
//        //判断是不是主线程
//         if(Looper.myLooper()==Looper.getMainLooper())
//         {
//             Handler handler =  getUIHandler();
//             if(handler!=null)
//             {
//                 handler.post(runnable);
//             }
//         }
//         else{
//            Handler handler =  getUIHandler();
//            if(handler!=null)
//            {
//                handler.post(runnable);
//            }
//         }
    }

    public static  void runUITaskDelay(Runnable runnable,long delayMillis)
    {
        Handler handler =  getUIHandler();
        if(handler!=null)
        {
            handler.postDelayed(runnable,delayMillis);
        }
//        //判断是不是主线程
//        if(Looper.myLooper()==Looper.getMainLooper())
//        {
//            Handler handler =  getUIHandler();
//            if(handler!=null)
//            {
//                handler.postDelayed(runnable,delayMillis);
//            }
//        }
//        else{
//            Handler handler =  getUIHandler();
//            if(handler!=null)
//            {
//                handler.postDelayed(runnable,delayMillis);
//            }
//        }
    }


    public static  void runWorkTask(Runnable runnable)
    {
        Handler handler =  getWorkHandler();
        if(handler!=null)
        {
            handler.post(runnable);
        }
//        //判断是不是主线程
//        if(Looper.myLooper()==Looper.getMainLooper())
//        {
//            Handler handler =  getWorkHandler();
//            if(handler!=null)
//            {
//                handler.post(runnable);
//            }
//        }
//        else{
//            Handler handler =  getWorkHandler();
//            if(handler!=null)
//            {
//                handler.post(runnable);
//            }
//        }
    }

    public static  void runWorkTaskDelay(Runnable runnable,long delayMillis)
    {
        Handler handler =  getWorkHandler();
        if(handler!=null)
        {
            handler.postDelayed(runnable,delayMillis);
        }
//        //判断是不是主线程
//        if(Looper.myLooper()==Looper.getMainLooper())
//        {
//            Handler handler =  getWorkHandler();
//            if(handler!=null)
//            {
//                handler.postDelayed(runnable,delayMillis);
//            }
//        }
//        else{
//            Handler handler =  getWorkHandler();
//            if(handler!=null)
//            {
//                handler.postDelayed(runnable,delayMillis);
//            }
//        }
    }


    /**
     * 比较长时间运行的任务，可以使用
     * @param runnable
     */
    public static  void runWorkTask2(Runnable runnable)
    {
        try{
            Thread thread = new Thread(runnable,"tempThread");
            thread.start();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
